package com.trg.generaltool.service.rabbitmq.topics;

import com.rabbitmq.client.*;
import com.trg.generaltool.util.rabbitmq.RabbitmqUtil;

import java.io.IOException;

/**
 * @ClassName: TopicsCustomer1
 * @Description:    动态路由-消费者一
 * @Author: tangrg
 * @Date: 2021/7/19 14:35
 * @Version: 1.0
 * @Return:
 **/
public class TopicsCustomer2 {

    public static void main(String[] args) throws IOException {

        //获取连接
        Connection connect = RabbitmqUtil.getConnect();
        //创建通道
        Channel channel = connect.createChannel();
        //绑定交换机
        channel.exchangeDeclare("topics","topic");
        //获取临时队列
        String queue = channel.queueDeclare().getQueue();
        //队列绑定交换机
        channel.queueBind(queue,"topics","user.#");
        //消费消息
        channel.basicConsume(queue,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者二："+new String(body));

            }
        });


    }
}
